<?php 
	namespace app\admin\controller;
	use think\Controller;
	use think\Session;
	use think\Db;


	//项目分类
	class Protype extends Allow
	{

        //项目分类列表
        public function getIndex()
		{
			//创建请求对象
            $request=request();
            //关键字查询
            $n=$request->param('tname');
            $c=$request->param('tcontent');
            //读取数据传给页面分配数据
	        $data=Db::table("protype")->where('tname',"like","%".$n."%")->where('tcontent',"like","%".$c."%")->paginate(10);
			return $this->fetch('protype/index',['data'=>$data,'request'=>$request]);
		}

        //加载添加页面
		public function getAdd()
		{
			return $this->fetch('protype/add');
		}
        
        //执行添加
		public function postInsert()
		{
	       	//创建请求对象
            $request=request();
            //获取请求信息
            $data=$request->only(['ttid','tname','tcontent']);
            //设置time字段时间值
            $data['ttime']=date('Y-m-d H:i:s');
            //判断数据是否插入成功
            if(Db::table('protype')->insert($data)){
                $this->success("数据添加成功","/admin/index");
            
            }else{
                $this->error("数据插入失败","/index/index");
            }

		}


        //加载修改页面
		public function getEdit()
		{
            //创建请求对象
            $request=request();
            //获取tid
            $id=$request->param('tid');
            //获取需要修改的数据
            $data=Db::table("protype")->where("tid",$id)->find();
			return $this->fetch('protype/edit',['data'=>$data]);
		}


	    //执行修改
        public function postUpdate()
        {
            //获取请求数据
        	$request=request();
        	$data=$request->only(['tid','tname','tcontent','ttime']);
            //设置查询索引
        	$id=$data['tid'];
            $d=Db::table("protype")->where("tid",$id)->find();
            //准备添加时的时间
        	$data['ttime']=$d['ttime'];
            //判断是否修改成功
        	if(Db::table('protype')->where('tid',"{$id}")->update($data))
        	{
        		$this->success('修改成功','/protype/index');
        	}else{
        		$this->error('修改失败','/protype/edit/etid/'.$id);
        	}
        }
        
        //执行删除
    	public function getDel()
		{
	       	//获取请求信息
            $request=request();
           	$id=$request->param('tid');
            //判断是否删除成功
    	    if(Db::table('protype')->where('tid',"{$id}")->delete())
    	    {
    	    	$this->success('删除成功','/protype/index');
    	    }else{
    	    	$this->error('删除失败','/protype/index');
    	    }
            
		}
	}
 ?>